package com.hyrluntan.forum.controller;

import com.hyrluntan.forum.common.AppResult;
import com.hyrluntan.forum.common.ResultCode;
import com.hyrluntan.forum.config.AppConfig;
import com.hyrluntan.forum.exception.ApplicationException;
import com.hyrluntan.forum.model.Article;
import com.hyrluntan.forum.model.ArticleReply;
import com.hyrluntan.forum.model.User;
import com.hyrluntan.forum.services.IArticleReplyService;
import com.hyrluntan.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "回复接⼝")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {
    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IArticleService articleService;

    /**
     * 回复帖⼦
     * @param articleId 帖⼦Id
     * @param content 回复内容
     * @return AppResult
     */
    @ApiOperation("回复帖⼦")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "帖⼦Id") @RequestParam(value = "articleId") @NonNull Long articleId,
                            @ApiParam(value = "回复内容") @RequestParam(value = "content") @NonNull String content) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString() + ", userId = " + user.getId());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 获取帖⼦信息
        Article article = articleService.selectById(articleId);
        if (article == null) {
            log.warn("要回复的帖⼦不存在. articleId = " + articleId);
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if (article.getState() != 0 || article.getDeleteState() != 0) {
            log.warn("帖⼦状态异常. articleId = " + articleId + ", state = " + article.getState() + ", delete state = " + article.getDeleteState());
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        // 构造数据
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);

        articleReplyService.create(articleReply);
        return AppResult.success();
    }

    /**
     * 帖子回复的列表
     *
     * @param articleId 帖⼦Id
     * @return AppResult
     */
    @ApiOperation("帖子回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam(value = "帖⼦Id") @RequestParam(value = "articleId") @NonNull Long articleId) {
            // 查询帖⼦是否存在
            Article article = articleService.selectById(articleId);
            if (article == null || article.getDeleteState() == 1) {
                log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
                throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
            }
            // 查询结果
            List<ArticleReply> result = articleReplyService.selectByArticleId(articleId);
            return AppResult.success(result);
    }
}
